<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XML 格式化工具</title>
    <link rel="stylesheet" href="../coding/common.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <style>
        /* 基础样式和变量 */
        :root {
            --primary-color: #4a6fa5;
            --secondary-color: #6c8ebd;
            --background-color: #f5f7fa;
            --card-background: #ffffff;
            --text-color: #333333;
            --border-color: #e0e0e0;
            --error-color: #e74c3c;
            --success-color: #2ecc71;
            --shadow-color: rgba(0, 0, 0, 0.1);
            --font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            --tip-background: #e3f2fd;
            --hover-color: #f0f8ff;
        }

        /* 暗黑模式变量 */
        [data-theme="dark"] {
            --primary-color: #5b8ad6;
            --secondary-color: #7fa3d7;
            --background-color: #1a1a2e;
            --card-background: #222438;
            --text-color: #e0e0e0;
            --border-color: #444444;
            --shadow-color: rgba(0, 0, 0, 0.3);
            --tip-background: #1a2a3a;
            --hover-color: #2a2a4e;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--background-color);
            color: var(--text-color);
            line-height: 1.6;
            transition: background-color 0.3s, color 0.3s;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 头部样式 */
        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--border-color);
        }

        header h1 {
            font-size: 2rem;
            color: var(--primary-color);
        }

        /* 头部控件区域 */
        .header-controls {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        /* 分享按钮 */
        .share-button {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 2px 6px;
            background-color: var(--secondary-color);
            color: white;
            border-radius: 15px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s, transform 0.1s;
        }

        .share-button:hover {
            background-color: var(--primary-color);
        }

        .share-button:active {
            transform: scale(0.98);
        }

        .share-button i {
            font-size: 16px;
        }

        /* 主题切换开关 */
        .theme-toggle {
            position: relative;
        }

        .theme-switch {
            opacity: 0;
            position: absolute;
        }

        .theme-switch-label {
            display: flex;
            align-items: center;
            justify-content: space-between;
            cursor: pointer;
            width: 50px;
            height: 26px;
            background-color: #83d8ff;
            border-radius: 100px;
            position: relative;
            transition: background-color 0.3s;
            padding: 0 5px;
        }

        .theme-switch:checked + .theme-switch-label {
            background-color: #4a4e69;
        }

        .theme-switch-label .fa-sun {
            color: #f39c12;
        }

        .theme-switch-label .fa-moon {
            color: #f1c40f;
        }

        .theme-switch-label .slider {
            position: absolute;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            transition: 0.3s;
            background-color: white;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
            left: 2px;
        }

        .theme-switch:checked + .theme-switch-label .slider {
            transform: translateX(24px);
        }

        /* 隐私提示框样式 */
        .privacy-notice {
            margin-bottom: 20px;
            animation: fadeIn 0.5s ease-in-out;
        }

        .privacy-notice-content {
            display: flex;
            align-items: center;
            background-color: var(--tip-background);
            border-left: 4px solid #2196f3;
            padding: 15px;
            border-radius: 4px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            position: relative;
        }

        .privacy-notice-content i.fa-shield-alt {
            font-size: 20px;
            color: #2196f3;
            margin-right: 15px;
        }

        .privacy-notice-content p {
            flex: 1;
            margin: 0;
            font-size: 14px;
            color: var(--text-color);
        }

        .close-notice {
            background: none;
            border: none;
            color: #90a4ae;
            cursor: pointer;
            font-size: 16px;
            padding: 5px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: color 0.2s;
        }

        .close-notice:hover {
            color: #546e7a;
            background: none;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 主内容区域 */
        main {
            flex: 1;
            display: flex;
            gap: 20px;
        }

        /* 左侧主要内容区域 */
        .main-content {
            flex: 2;
            background-color: var(--card-background);
            border-radius: 8px;
            box-shadow: 0 4px 6px var(--shadow-color);
            padding: 25px;
        }

        .main-content h2 {
            color: var(--primary-color);
            margin-bottom: 20px;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .input-section {
            margin-bottom: 20px;
        }

        .input-section label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--text-color);
        }

        .input-textarea {
            width: 100%;
            min-height: 610px;
            padding: 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            font-size: 14px;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            background-color: var(--card-background);
            color: var(--text-color);
            resize: vertical;
            transition: border-color 0.3s;
            white-space: pre;
            overflow-wrap: normal;
            overflow-x: auto;
        }

        .input-textarea:focus {
            outline: none;
            border-color: var(--primary-color);
        }

        .button-group {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--secondary-color);
            transform: translateY(-1px);
        }

        .btn-success {
            background-color: var(--success-color);
            color: white;
        }

        .btn-success:hover {
            background-color: #27ae60;
            transform: translateY(-1px);
        }

        /* 右侧侧边栏 */
        .sidebar {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .sidebar-card {
            background-color: var(--card-background);
            border-radius: 8px;
            box-shadow: 0 4px 6px var(--shadow-color);
            padding: 20px;
        }

        .sidebar-card h3 {
            color: var(--primary-color);
            margin-bottom: 15px;
            font-size: 1.2rem;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .sidebar-card p {
            font-size: 14px;
            line-height: 1.5;
            margin-bottom: 10px;
        }

        .sidebar-card ul {
            list-style: none;
            padding: 0;
        }

        .sidebar-card li {
            font-size: 13px;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .sidebar-card li i {
            color: var(--success-color);
            font-size: 12px;
        }

        /* 通知样式 */
        .notification {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: var(--success-color);
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            transform: translateY(100px);
            opacity: 0;
            transition: transform 0.3s, opacity 0.3s;
        }

        .notification.show {
            transform: translateY(0);
            opacity: 1;
        }

        .notification.error {
            background-color: var(--error-color);
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .privacy-notice-content {
                flex-direction: column;
                text-align: center;
                padding: 15px 10px;
            }
            
            .privacy-notice-content i.fa-shield-alt {
                margin-right: 0;
                margin-bottom: 10px;
            }
            
            .close-notice {
                position: absolute;
                top: 5px;
                right: 5px;
            }

            header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }

            main {
                flex-direction: column;
            }

            .button-group {
                justify-content: center;
            }
        }
    </style>
    <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
</head>
<body>
    <header>
        <div class="header-container">
            <div class="header-left">
                <div class="logo">
                    <span><a href="https://geekdaxue.co/" target="_blank"><img src="https://geekdaxue.co/static/images/logo_platypus.png" height="30px"></a></span>
                </div>
                <nav class="main-navigation">
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            JSON工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../json/index.html">JSON 格式化</a>
                            <a href="../json/parser.html">JSON 格式校验</a>
                            <a href="../json/excel.html">JSON 转 Excel/CSV</a>
                            <a href="../json/jsonxml.html">JSON 与 XML 转换</a>
                            <a href="../json/editor.html">JSON 编辑器</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            编码转换 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../coding/base64.html">Base64 编码</a>
                            <a href="../coding/urlencoding.html">URL 编码</a>
                            <a href="../coding/md5.html">MD5 加密</a>
                            <a href="../coding/unicode.html">Unicode 转换</a>
                            <a href="../coding/hex_convert.html">进制转换</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            文本工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../txt/diff.html">文本对比</a>
                            <a href="../txt/replace.html">文本替换</a>
                            <a href="../txt/remove.html">空白行移除</a>
                            <a href="../txt/cn_trans.html">繁简体转换</a>
                            <a href="../txt/html_format.html">HTML 格式化</a>
                            <a href="../txt/xml_format.html">XML 格式化</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            图片工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../img/2svg.html">图片转 SVG</a>
                            <a href="../img/qr_code.html">二维码生成</a>
                            <a href="../img/barcode.html">条形码生成</a>
                        </div>
                    </div>
                </nav>
            </div>
            <div class="header-controls">
                <div class="theme-toggle">
                    <input type="checkbox" id="theme-switch" class="theme-switch">
                    <label for="theme-switch" class="theme-switch-label">
                        <i class="fas fa-sun"></i>
                        <i class="fas fa-moon"></i>
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="share-button" id="share-button" title="分享给朋友">
                    <i class="fas fa-share-alt"></i>
                    <span>分享</span>
                </div>
            </div>
        </div>
    </header>
    <div class="container">
        <div id="privacy-notice" class="privacy-notice">
            <div class="privacy-notice-content">
                <i class="fas fa-shield-alt"></i>
                <p>安全提示：您的数据仅在本地浏览器中处理，不会上传到服务器，请放心使用！</p>
                <button id="close-notice" class="close-notice" title="关闭提示">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        </div>
        
        <main>
            <!-- 左侧主要内容区域 -->
            <div class="main-content">
                <h2>
                    <i class="fas fa-file-code"></i>
                    XML 格式化工具
                </h2>
                
                <div class="input-section">
                    <label for="xml-input">请输入要格式化的 XML 代码：</label>
                    <textarea id="xml-input" class="input-textarea" placeholder="请在此输入您要格式化的 XML 代码..."></textarea>
                </div>
                
                <div class="button-group">
                    <button id="format-xml" class="btn btn-primary">
                        <i class="fas fa-magic"></i>
                        格式化 XML
                    </button>
                    <button id="copy-xml" class="btn btn-success">
                        <i class="fas fa-copy"></i>
                        复制代码
                    </button>
                </div>
            </div>
            
            <!-- 右侧侧边栏 -->
            <div class="sidebar">
                <div class="sidebar-card">
                    <div class="wwads-cn wwads-vertical" data-id="321" style="max-width:200px"></div>
                </div>
                <div class="sidebar-card">
                    <h3>
                        <i class="fas fa-info-circle"></i>
                        使用说明
                    </h3>
                    <p>这是一个简单易用的 XML 代码格式化工具，可以美化和整理您的 XML 代码。</p>
                    <ul>
                        <li><i class="fas fa-check"></i> 自动缩进和换行</li>
                        <li><i class="fas fa-check"></i> 标签对齐和美化</li>
                        <li><i class="fas fa-check"></i> 属性格式化</li>
                        <li><i class="fas fa-check"></i> 代码结构清晰化</li>
                        <li><i class="fas fa-check"></i> 一键复制格式化结果</li>
                        <li><i class="fas fa-check"></i> 本地处理，保护隐私</li>
                        <li><i class="fas fa-check"></i> 支持大量 XML 代码</li>
                    </ul>
                </div>
                
                <div class="sidebar-card">
                    <h3>
                        <i class="fas fa-lightbulb"></i>
                        格式化说明
                    </h3>
                    <p>本工具会自动为您的 XML 代码添加适当的缩进、换行和空格，使代码更加易读和美观。</p>
                    <p>支持所有标准的 XML 标签和属性，包括命名空间、CDATA 等特殊结构。</p>
                    <p>注意：格式化过程中会保留原有的文本内容，但可能会调整空白字符。</p>
                </div>
                
                <div class="sidebar-card">
                    <h3>
                        <i class="fas fa-keyboard"></i>
                        快捷键
                    </h3>
                    <ul>
                        <li><i class="fas fa-check"></i> Ctrl + Enter：格式化 XML</li>
                        <li><i class="fas fa-check"></i> Ctrl + C：复制代码</li>
                    </ul>
                </div>
            </div>
        </main>
    </div>
    <footer>
        <div class="footer-container">
            <div class="footer-main">
                <div class="footer-section about-us">
                    <h3>关于我们</h3>
                    <p>我们提供一系列高效、便捷的在线工具，帮助开发者、非技术人员提高工作效率，解决日常开发中遇到的各种问题。</p>
                    <p>Build With <i class="fa-solid fa-heart" style="color: red;"></i> For SF.</p>
                </div>
                <div class="footer-section links">
                    <h3>工具导航</h3>
                    <div class="links-columns">
                        <ul>
                            <li><a href="../json/index.html">JSON 工具</a></li>
                            <li><a href="../img/index.html">图片处理</a></li>
                            <li><a href="#">开发工具</a></li>
                        </ul>
                        <ul>
                            <li><a href="../coding/index.html">编码转换</a></li>
                            <li><a href="../txt/index.html">文本处理</a></li>
                            <li><a href="#">更多工具</a></li>
                        </ul>
                    </div>
                </div>
                <div class="footer-section contact">
                    <h3>联系我们</h3>
                    <p>如果您有任何建议或问题，欢迎联系我们。</p>
                    <div class="social-links">
                        <a href="#" title="GitHub"><i class="fab fa-github"></i></a>
                        <a href="#" title="邮箱"><i class="fas fa-envelope"></i></a>
                        <a href="#" title="微信"><i class="fab fa-weixin"></i></a>
                    </div>
                </div>
            </div>
            <div class="footer-bottom">
                <p>&copy; 2024 在线工具集. All rights reserved.</p>
            </div>
        </div>
    </footer>
    
    <!-- 通知元素 -->
    <div id="notification" class="notification"></div>
    
    <script>
        // XML 格式化函数
        function formatXML(xml) {
            // 移除多余的空白字符
            xml = xml.replace(/\s+/g, ' ').trim();
            
            let formatted = '';
            let indent = 0;
            const indentSize = 2;
            
            // 解析 XML 标签和内容
            const xmlRegex = /<\/?[^>]+>|[^<]+/g;
            let match;
            
            while ((match = xmlRegex.exec(xml)) !== null) {
                const token = match[0].trim();
                if (!token) continue;
                
                if (token.startsWith('<?')) {
                    // XML 声明
                    formatted += token + '\n';
                } else if (token.startsWith('<!--')) {
                    // 注释
                    formatted += ' '.repeat(indent * indentSize) + token + '\n';
                } else if (token.startsWith('</')) {
                    // 结束标签
                    indent = Math.max(0, indent - 1);
                    formatted += ' '.repeat(indent * indentSize) + token + '\n';
                } else if (token.startsWith('<')) {
                    // 开始标签或自闭合标签
                    const isSelfClosing = token.endsWith('/>');
                    const isCDATA = token.includes('<![CDATA[');
                    
                    if (isCDATA) {
                        // CDATA 特殊处理
                        formatted += ' '.repeat(indent * indentSize) + token + '\n';
                    } else {
                        formatted += ' '.repeat(indent * indentSize) + token + '\n';
                        if (!isSelfClosing) {
                            indent++;
                        }
                    }
                } else {
                    // 文本内容
                    if (token.length > 0) {
                        formatted += ' '.repeat(indent * indentSize) + token + '\n';
                    }
                }
            }
            
            // 清理多余的空行
            formatted = formatted.replace(/\n\s*\n/g, '\n').trim();
            
            return formatted;
        }
        
        // 显示通知函数
        function showNotification(message, isError = false) {
            const notification = document.getElementById('notification');
            if (notification) {
                notification.textContent = message;
                notification.className = 'notification show' + (isError ? ' error' : '');
                setTimeout(() => {
                    notification.classList.remove('show');
                }, 3000);
            }
        }
        
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const themeSwitch = document.getElementById('theme-switch');
            const privacyNotice = document.getElementById('privacy-notice');
            const closeNoticeBtn = document.getElementById('close-notice');
            const shareButton = document.getElementById('share-button');
            const xmlInput = document.getElementById('xml-input');
            const formatBtn = document.getElementById('format-xml');
            const copyBtn = document.getElementById('copy-xml');
            
            // 检查本地存储中的主题设置
            if (localStorage.getItem('theme') === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
                themeSwitch.checked = true;
            }
            
            // 主题切换功能
            themeSwitch.addEventListener('change', function() {
                if (this.checked) {
                    document.documentElement.setAttribute('data-theme', 'dark');
                    localStorage.setItem('theme', 'dark');
                } else {
                    document.documentElement.removeAttribute('data-theme');
                    localStorage.setItem('theme', 'light');
                }
            });
            
            // 隐私提示框关闭功能
            closeNoticeBtn.addEventListener('click', function() {
                privacyNotice.style.display = 'none';
                localStorage.setItem('privacyNoticeClosed', 'true');
            });
            
            // 检查是否需要显示隐私提示
            if (localStorage.getItem('privacyNoticeClosed') === 'true') {
                privacyNotice.style.display = 'none';
            }
            
            // 分享功能
            shareButton.addEventListener('click', function() {
                const url = window.location.href;
                navigator.clipboard.writeText(url).then(() => {
                    showNotification('已复制网址，赶快让更多的小伙伴知道我吧！');
                }).catch(() => {
                    showNotification('复制失败，请手动复制网址', true);
                });
            });
            
            // XML 格式化功能
            formatBtn.addEventListener('click', function() {
                const input = xmlInput.value;
                if (!input.trim()) {
                    showNotification('请先输入要格式化的 XML 代码', true);
                    return;
                }
                
                try {
                    const formatted = formatXML(input);
                    xmlInput.value = formatted;
                    // 将横向滚动条定位到最左侧
                    xmlInput.scrollLeft = 0;
                    showNotification('XML 格式化完成！');
                } catch (error) {
                    console.error('格式化失败:', error);
                    showNotification('格式化失败，请检查 XML 代码是否正确', true);
                }
            });
            
            // 复制功能
            copyBtn.addEventListener('click', function() {
                const content = xmlInput.value;
                if (!content.trim()) {
                    showNotification('没有可复制的内容', true);
                    return;
                }
                
                navigator.clipboard.writeText(content).then(() => {
                    showNotification('已复制到剪贴板！');
                }).catch(() => {
                    showNotification('复制失败，请手动复制', true);
                });
            });
            
            // 键盘快捷键
            document.addEventListener('keydown', function(e) {
                if (e.ctrlKey) {
                    switch(e.key) {
                        case 'Enter':
                            e.preventDefault();
                            formatBtn.click();
                            break;
                        case 'c':
                            if (document.activeElement === xmlInput) {
                                e.preventDefault();
                                copyBtn.click();
                            }
                            break;
                    }
                }
            });
        });
    </script>
</body>
</html>